package org.oslims.client.presenter;

import java.util.List;

import org.oslims.client.AsyncCall;
import org.oslims.client.MainEventBus;
import org.oslims.client.service.RequestServiceAsync;
import org.oslims.client.uicomponents.WidgetUtils;
import org.oslims.client.uicomponents.editbox.SelectRequestBox;
import org.oslims.client.view.impl.SampleEditorRootViewImpl;
import org.oslims.domain.Request;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.mvp4g.client.annotation.InjectService;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter(view = SampleEditorRootViewImpl.class)
public class SampleRootPresenter extends BasePresenter<SampleEditorRootViewImpl, MainEventBus> {

	private RequestServiceAsync requestService;
	private Request currentRequest;

	private HandlerRegistration handlerRegistration;

	public SampleRootPresenter() {

	}

	@InjectService
	public void setRequestService(RequestServiceAsync requestService) {
		this.requestService = requestService;
	}

	public Request getCurrentRequest() {
		return currentRequest;
	}

	public void setCurrentRequest(Request currentRequest) {
		this.currentRequest = currentRequest;
	}

	@Override
	public void bind() {
		eventBus.inizializeSampleManagement(view.getSampleTabPanel());
		eventBus.inizializeMixtureManagement(view.getSampleTabPanel());
	}

	public void onShowRootSampleManagement(DockLayoutPanel mainWidget) {

		WidgetUtils.removeCenterWidget(mainWidget);

		final DockLayoutPanel mainWidget2 = mainWidget;

		requestService.getAllRequest(new AsyncCall<List<Request>>() {

			@Override
			protected void onSuccessE(List<Request> result) {

				view.setSelectRequestBox(new SelectRequestBox(result));

				view.getSelectRequestBox().getOKButton().addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {

						String requestCode = view.getSelectRequestBox().getRequestCode().getValue();

						GWT.log(requestCode);
						showSamplesForRequest(mainWidget2, requestCode);
					}
				});

				view.getSelectRequestBox().getCancelButton().addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						view.getSelectRequestBox().hide();
					}
				});

				view.getSelectRequestBox().show(result);
			}

			@Override
			protected void onFailureE(Throwable caught) {
				eventBus.error(caught.getMessage());
			}
		});

	}

	public void onShowRootSampleManagementByRequest(DockLayoutPanel mainWidget, String requestCode) {

		WidgetUtils.removeCenterWidget(mainWidget);

		final DockLayoutPanel mainWidget2 = mainWidget;

		showSamplesForRequest(mainWidget2, requestCode);
	}

	private void showSamplesForRequest(final DockLayoutPanel mainWidget2, String requestCode) {
		requestService.findByCode(requestCode, new AsyncCall<Request>() {

			@Override
			protected void onSuccessE(Request result) {
				if (result != null) {
					currentRequest = result;

					view.getHeaderLabel().setText("Define Samples for Request " + currentRequest.getCode());

					mainWidget2.add(view.getViewWidget());

					if (handlerRegistration == null) {
						handlerRegistration = view.getSampleTabPanel().addSelectionHandler(new SelectionHandler<Integer>() {

							@Override
							public void onSelection(SelectionEvent<Integer> event) {
								if (event.getSelectedItem() == 0) {
									eventBus.refreshSampleView(currentRequest);
								}
								if (event.getSelectedItem() == 1) {
									eventBus.refreshMixtureView(currentRequest);
								}
							}
						});
						view.getSampleTabPanel().selectTab(0);
					} else {
						view.getSampleTabPanel().selectTab(0);
					}
				} else {
					eventBus.error("Request not found!");
				}
			}

			@Override
			protected void onFailureE(Throwable caught) {
				eventBus.equals(caught.getMessage());
			}
		});
	}

}
